﻿using System;
using System.Collections.Generic;
using HTB.DevFx.Core;
using HTB.DevFx.Log;
using HTB.DevFx.Utils;
using Maple.Weixin.MP.Entities.Messages;

namespace Maple.Weixin.MP.Monitors
{
    public abstract class MonitorServiceBase : TimerBase, IMonitorService, IDisposable
    {
        protected virtual Queue<RequestMessageBase> RequestMessageQueue { get; private set; }
        protected virtual bool Enabled { get; set; }

        protected MonitorServiceBase() {
            this.RequestMessageQueue = new Queue<RequestMessageBase>(1024);
        }

        // 定期引发记录监控数据的操作
        protected override void OnTimer() {
            var messageList = new List<RequestMessageBase>();
            if (this.RequestMessageQueue.Count > 0) {
                lock (RequestMessageQueue) {
                    var message = RequestMessageQueue.Dequeue();
                    messageList.Add(message);
                }
            }
            if (!this.Enabled || messageList.Count <= 0)
                return;

            var messages = messageList.ToArray();
            try {
                WriteReqeustMessages(messages);
            }
            catch (Exception ex) {
                LogService.WriteLog(ex.Message + ex.StackTrace);
            }
        }

        protected virtual void WriteReqeustMessages(RequestMessageBase[] requestMessages) {
            foreach (var requestMessage in requestMessages) {
                LogService.WriteLog(JsonHelper.ToJson(requestMessage, false));
            }
        }

        protected virtual void ReordRequestMessage(RequestMessageBase requstMessage) {
            lock (RequestMessageQueue)
                RequestMessageQueue.Enqueue(requstMessage);
            this.StartTimer();
        }

        #region Implementation of IMonitorService

        /// <summary>
        /// 记录请求消息。
        /// </summary>
        /// <param name="requestMessage">所请求的消息。</param>
        void IMonitorService.RecordRequestMessage(RequestMessageBase requestMessage)
        {
            ReordRequestMessage(requestMessage);
        }

        #endregion

        #region Implementation of IDisposable

        private bool disposed;

        protected virtual void Disponse() {
            if (!this.disposed) {
                this.disposed = true;
                this.OnTimer();
            }
        }

        void IDisposable.Dispose() {
            this.Disponse();
            GC.SuppressFinalize(this);
        }

        ~MonitorServiceBase() {
            this.Disponse();
        }

        #endregion
    }

    public class MonitorService : MonitorServiceBase, IInitializable
    {
        protected virtual void Init()
        {
            this.Enabled = true;
        }

        #region Implementation of IInitializable

        /// <summary>
        /// 初始化
        /// </summary>
        void IInitializable.Init() {
            this.Init();
        }

        #endregion
    }
}
